package ren.renshuaipeng.util.study.demo.conf;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ren.renshuaipeng.util.mybatis.MyBatisRepository;


import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;

/**
 * ClassName: DruidConfiguration<br/>
 * Function: 数据源配置. <br/>
 * Date:     2015/12/14 10:17<br/>
 *
 * @author 任帅鹏（作者）
 * @version V1.00 （版本号）
 * @see
 * @since JDK 1.8
 */
@Configuration
@MapperScan(basePackages = "ren.renshuaipeng.study.demo",annotationClass = MyBatisRepository.class)
public class DruidConf {

    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean statViewServlet= new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        statViewServlet.addInitParameter("resetEnable","true");
        statViewServlet.addInitParameter("loginUsername","admin");
        statViewServlet.addInitParameter("loginPassword","admin");
        return statViewServlet;
    }

    @Bean(name="dataSource",initMethod = "init",destroyMethod="close")
    public DataSource GetDataSource(
                          @Value("${spring.datasource.driver-class-name}") String driver,
                          @Value("${spring.datasource.url}") String url,
                          @Value("${spring.datasource.username}") String username,
                          @Value("${spring.datasource.password}") String password) throws SQLException{
        DruidDataSource druidDataSource = new DruidDataSource();
        //1.基本
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        druidDataSource.setFilters("stat, wall");
        //2.高级设置
        return druidDataSource;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.addInitParameter("sessionStatMaxCount","2000");
        filterRegistrationBean.addInitParameter("sessionStatEnable","true");
        filterRegistrationBean.addInitParameter("principalSessionName","session_user_key");
        filterRegistrationBean.addInitParameter("principalSessionName","profileEnable");
        return filterRegistrationBean;
    }

//    @Bean
//    public MapperScannerConfigurer getMapperScannerConfigurer(){
//        MapperScannerConfigurer configurer=new MapperScannerConfigurer();
//        configurer.setBasePackage("ren.renshuaipeng.search");
//        configurer.setAnnotationClass(MyBatisRepository.class);
//        return configurer;
//    }

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("dataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);

//        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//
//        sqlSessionFactoryBean.setMapperLocations(resolver
//                .getResources("classpath:/mapper/*Mapper.xml"));

        //使用分页插件
        PageHelper pageHelper=new PageHelper();
        Properties properties=new Properties();
        properties.put("dialect","mysql");
        pageHelper.setProperties(properties);
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{pageHelper});
        return sqlSessionFactoryBean.getObject();
    }
}
